package dao.impl;

import dao.BaseDao;
import dao.IUsersDao;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.User;
import utils.JDBCUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements IUsersDao {
    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean insertUser(User user) {
        return update("insert into t_user(username,password,real_name,phone,address,admin) value(?,?,?,?,?,?)", user.getUsername(), user.getPassword(), user.getReal_name(), user.getPhone(), user.getAddress(), user.isAdmin() ? 1 : 0) > 1;
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<User> queryAllUser() {
        return queryForList(User.class, "select id,username,real_name,password,phone,address,admin from t_user");
    }


    /**
     * 根据用户名和密码查询用户
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User queryUserByUsernameAndPassword(String username, String password) {
        return queryForOne(User.class,
                "select id,username,real_name,password,phone,address,admin from t_user where username=? and password=?",
                username, password);
    }

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @Override
    public User queryUserByUsername(String username) {
        return queryForOne(User.class, "select id,username,real_name,password,phone,address,admin from t_user where username=?", username);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User queryUserById(int id) {
        return queryForOne(User.class, "select id,username,real_name,password,phone,address,admin from t_user where id=?", id);
    }
}
